package remote.controls
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import remote.geom.Size;
	
	/**
	 * 步进控件
	 * @author 银魂飞雪
	 * @createDate 2011-4-17
	 */
	public class NumericStepper extends Component implements ICounting
	{
		private var _upButton:Button;
		private var _downButton:Button;
		private var _textInput:TextInput;
		
		private var _buttonWidth:Number;
		
		private var _value:Number = 0;
		private var _minValue:Number = 0;
		private var _maxValue:Number = 100;
		private var _stepSize:Number = 1;
		public function NumericStepper()
		{
			super();
		}
		
		override protected function initProperty():void
		{
			super.initProperty();
			_buttonWidth = 30;
			_width = 110;
			_height = 24;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function updateSize():void
		{
			var oldSize:Size = new Size(width, height);
			super.updateSize();
			if(!oldSize.equal(new Size(width, height)))
				this.updateChildren();
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override public function getMeasureSize():Size
		{
			return new Size(_buttonWidth + 30, _textInput.height);
		}
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createChildren():void
		{
			_upButton = new Button();
			_downButton = new Button();
			_textInput = new TextInput();
			_textInput.textField.restrict = "0-9";
			
			_upButton.addEventListener(MouseEvent.CLICK, upButton_clickHandler);
			_downButton.addEventListener(MouseEvent.CLICK, downButton_clickHandler);
			_textInput.addEventListener(FocusEvent.FOCUS_OUT, txtValue_focusOutHandler);
			
			addChild(_textInput);
			addChild(_upButton);
			addChild(_downButton);
			updateChildren();
		}
		
		private function addValidate():void
		{
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		private function enterFrameHandler(e:Event):void
		{
			removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			_value = getRightValue(_value);
			_textInput.text = _value.toString();
		
		}
		
		private function getRightValue(value:Number):Number
		{
			if(minValue > maxValue)
				throw new Error("最小值大于最大值");
			if(value < minValue)
				return minValue;
			if(value > maxValue)
				return maxValue;
			return value;
		}
		
		override protected function updateChildren():void
		{
			_textInput.setSize(Math.max(0, width - _buttonWidth), height);
			var btnWidth:Number = Math.min(width, _buttonWidth);
			_upButton.setSize(btnWidth, height / 2);
			_downButton.setSize(btnWidth, height / 2);
			_upButton.x = _textInput.width;
			_downButton.move(_textInput.width, height / 2);
			
		}
		
		private function upButton_clickHandler(e:MouseEvent):void
		{
			if(value < maxValue)
				value += stepSize;
		}
		
		private function downButton_clickHandler(e:MouseEvent):void
		{
			if(value > minValue)
				value -= stepSize;
		}
		
		private function txtValue_focusOutHandler(e:FocusEvent):void
		{
			value = Number(_textInput.text);
		}
		
		public function get minValue():Number
		{
			return _minValue;
		}
		
		public function set minValue(value:Number):void
		{
			if(_minValue != value)
			{
				_minValue = value;
				addValidate();
			}
		}
		
		public function get maxValue():Number
		{
			return _maxValue;
		}
		
		public function set maxValue(value:Number):void
		{
			if(_maxValue != value)
			{
				_maxValue = value;
				addValidate();
			}
		}
		
		public function get value():Number
		{
			return _value;
		}
		
		public function set value(value:Number):void
		{
			if(value != this.value)
			{
				_value = value;
				addValidate();
			}
		}

		public function get stepSize():Number
		{
			return _stepSize;
		}

		public function set stepSize(value:Number):void
		{
			_stepSize = value;
		}

		public function get buttonWidth():Number
		{
			return _buttonWidth;
		}

		public function set buttonWidth(value:Number):void
		{
			if(_buttonWidth != value)
			{
				_buttonWidth = value;
				this.updateChildren();
			}
		}

		public function get textInput():TextInput
		{
			return _textInput;
		}

		public function get upButton():Button
		{
			return _upButton;
		}

		public function get downButton():Button
		{
			return _downButton;
		}


	}
}